package com.uni.crawl.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.uni.crawl.common.base.BaseEntity;
import com.uni.crawl.common.base.BasePageQuery;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class PageQueryUtils {

    public static <T> Page<T> getPage(BasePageQuery basePageQuery) {
        return new Page<>(basePageQuery.getPageNum(), basePageQuery.getPageSize());
    }

    public static <T extends BaseEntity> void wrapTimeRange(BasePageQuery pageQuery, MPJLambdaWrapper<T> wp) {
        wp.ge(ObjectUtil.isNotNull(pageQuery.getCreateTimeStart()), T::getCreateTime, pageQuery.getCreateTimeStart());
        wp.lt(ObjectUtil.isNotNull(pageQuery.getCreateTimeEnd()), T::getCreateTime, pageQuery.getCreateTimeEnd());
    }

    public static <T extends BaseEntity, R> void wrapTimeRange(MPJLambdaWrapper<T> wp, SFunction<R, ?> column,BasePageQuery pageQuery) {
        wp.ge(ObjectUtil.isNotNull(pageQuery.getCreateTimeStart()), column, pageQuery.getCreateTimeStart());
        wp.lt(ObjectUtil.isNotNull(pageQuery.getCreateTimeEnd()), column, pageQuery.getCreateTimeEnd());
    }

}
